package com.ruoyi.system.service.impl.wechat;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.wechat.WechatTask;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.system.domain.wechat.request.vo.FilterForWechatTaskListVO;
import com.ruoyi.system.mapper.wechat.WechatTaskMapper;
import com.ruoyi.system.service.wechat.IWechatTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 微信小程序用户
 *
 * @author jiexiang.sun/Jason
 * @version 1.0.0
 * @create 2022-03-03 20:41
 **/
@Service
@Slf4j
public class WechatTaskServiceImpl extends ServiceImpl<WechatTaskMapper, WechatTask> implements IWechatTaskService {

    @Autowired
    private WechatTaskMapper taskMapper;

    @Override
    public List<WechatTask> list(FilterForWechatTaskListVO params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        LambdaQueryWrapper<WechatTask> taskQW = new LambdaQueryWrapper<>();
        taskQW.eq(WechatTask::getCreateUserDeptId, params.getUserDeptId())
                .eq(WechatTask::getDelFlag, UserStatus.OK.getCode())
                .ge(ObjectUtil.isNotNull(params.getStartTime()), WechatTask::getStartTime, params.getStartTime())
                .le(ObjectUtil.isNotNull(params.getEndTime()), WechatTask::getEndTime, params.getEndTime())
                .and(StrUtil.isNotBlank(params.getTaskName()), i -> {
                    i.like(StrUtil.isNotBlank(params.getTaskName()), WechatTask::getTaskCode, params.getTaskName())
                            .or()
                            .like(StrUtil.isNotBlank(params.getTaskName()), WechatTask::getTaskName, params.getTaskName());
                })
                .orderByDesc(WechatTask::getCreateTime);

        List<WechatTask> tasks = taskMapper.selectList(taskQW);
        if (CollectionUtil.isEmpty(tasks)) {
            return Collections.EMPTY_LIST;
        }

        return tasks;
    }
}
